package app.vista;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vo.ArticuloVO;
import app.controlador.ControladorNuevoArticulo;
import framework.modelo.Businessdelegate;
import framework.vista.Vista;

public class NuevoArticulo extends Vista implements ActionListener {
	private static final long serialVersionUID = 1L;
	int codigoArticulo = 0;

	public NuevoArticulo(Businessdelegate bd) {
		super(bd);
	}

	public void inicializar() {
		JFC jfc = new JFC();
		ArticuloVO articulovo;

		if (jfc.getSelectedFile() != null) {
			articulovo = ((ControladorNuevoArticulo) (this.getControlador()))
					.obtenerNuevoArticuloXML(jfc.getSelectedFile()
							.getAbsolutePath());
			((ControladorNuevoArticulo) (this.getControlador()))
					.guardarArticulo(articulovo);
			codigoArticulo = articulovo.getCodigo();
			System.out.println(codigoArticulo + " es el codigoArticulo");
		}

		if (codigoArticulo != 0) {
			componentes();
			propiedades();
			centrarPantalla();

			this.setVisible(true);
		}
	}

	@SuppressWarnings("unchecked")
	public void componentes() {
		ArticuloVO articulovo;
		String[] fila = new String[8];

		Container c = this.getContentPane();
		c.setLayout(new BorderLayout());

		JPanel norte = new JPanel(new GridLayout(1, 2, 0, 0));

		ImageIcon logoZara = new ImageIcon("./data/logo2.jpg", "ZARA");
		JLabel etiqueta = new JLabel(logoZara);

		norte.add(etiqueta);

		c.add(norte, BorderLayout.NORTH);

		articulovo = ((ControladorNuevoArticulo) (this.getControlador()))
				.obtenerArticulo(codigoArticulo);
		System.out.println("Obtiene el articulovo desde el Businessdelegate");

		DefaultTableModel defaulttablemodel = new DefaultTableModel();
		defaulttablemodel.addColumn("Codigo");
		defaulttablemodel.addColumn("Linea");
		defaulttablemodel.addColumn("Descripcion");
		defaulttablemodel.addColumn("Talle");
		defaulttablemodel.addColumn("Color");
		defaulttablemodel.addColumn("Seccion");
		defaulttablemodel.addColumn("Pvu");
		defaulttablemodel.addColumn("Origen");

		fila[0] = "Codigo";
		fila[1] = "Linea";
		fila[2] = "Descripcion";
		fila[3] = "Talle";
		fila[4] = "Color";
		fila[5] = "Seccion";
		fila[6] = "Pvu";
		fila[7] = "Origen";
		defaulttablemodel.addRow(fila);

		if (articulovo != null) {
			fila[0] = (String.valueOf(articulovo.getCodigo()));
			fila[1] = (String.valueOf(articulovo.getLinea()));
			fila[2] = (String.valueOf(articulovo.getDescripcion()));
			fila[3] = (String.valueOf(articulovo.getTalle()));
			fila[4] = (String.valueOf(articulovo.getColor()));
			fila[5] = (String.valueOf(articulovo.getSeccion()));
			fila[6] = (String.valueOf(articulovo.getPvu()));
			fila[7] = (String.valueOf(articulovo.getOrigen()));
			defaulttablemodel.addRow(fila);
		}

		JTable table = new JTable(defaulttablemodel);
		c.add(table, BorderLayout.CENTER);

		JPanel sur = new JPanel(new FlowLayout());
		JButton volver = new JButton("Volver");
		volver.addActionListener(this);
		volver.setActionCommand("btnVolver");

		sur.add(volver);

		c.add(sur, BorderLayout.SOUTH);
	}

	public void propiedades() {
		setTitle("Recepci�n de Informaci�n del Nuevo Art�culo");
		setLocation(100, 100);
		setIconImage(new ImageIcon("./data/icon.jpg").getImage());
		// setResizable(false);
		pack();
	}

	public void centrarPantalla() {
		Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension ventana = this.getSize();

		this.setLocation((pantalla.width - ventana.width) / 2,
				(pantalla.height - ventana.height) / 2);
	}

	public void actionPerformed(ActionEvent e) {
		// btnVolver presionado
		if ("btnVolver".equals(e.getActionCommand())) {
			this.setVisible(false);
		}
	}
}
